<template>
	<view class="goodsinfo">
		<!-- 轮播图 -->
		<view class="swiper-holder">
			<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000">
				<swiper-item v-for="item in goodsInfo.skuList" :key="item.id">
					<image :src="item.pic" mode=""></image>
				</swiper-item>
			</swiper>
		</view>

		<!-- 商品信息展示 -->
		<view class="info-holder">
			<view class="collect-btn" @tap="changeCollect">
				<image v-if="!isCollect" src="../../static/icons/collect.png" mode=""></image>
				<image v-else src="../../static/icons/collect1-active.png" mode=""></image>
			</view>
			<view class="price-holder">
				<view class="price-box">
					<text class="pre price">￥</text>
					<text class="price">{{goodsInfo.price}}</text>
					<text class="pre price">.00</text>
					<text class="dis price">￥{{goodsInfo.promotionPrice}}</text>
				</view>
				<view class="card">
					没有促销使用原价
				</view>
			</view>
			<view class="name">
				{{goodsInfo.name}}
			</view>
			<view class="desc">
				{{goodsInfo.description}}
			</view>
		</view>

		<!-- 商品富文本 -->
		<view class="rich-text">
			<mp-html :content="goodsInfo.detailMobileHtml" :tag-style="{img:'width:100%'}" />
		</view>

		<!-- 底部功能栏 -->
		<view class="bottom">
			<view class="left">
				<image src="../../static/icons/kf.png" mode=""></image>
			</view>

			<view class="btn-holder">
				<view class="btn add-cart" @tap='addToCart'>
					添加至购物车
				</view>
				<view class="btn buy-now" @tap='buyNow'>
					立即购买
				</view>
			</view>
		</view>

		<!-- 点击弹框 -->
		<view class="pop-box" v-if="popShow" @touchmove.stop.prevent="moveHandle" @tap="closePop">
			<view class="content" @tap.stop="moveHandle">
				<view class="img-holder">
					<image :src="goodsInfo.pic" mode=""></image>
				</view>

				<view class="close-btn" @tap="closePop">
					<image src="../../static/icons/x.png" mode=""></image>
				</view>

				<view class="name-holder">
					<view class="name">
						{{goodsInfo.name}}
					</view>
					<view class="stock-count">
						库存数量：{{goodsInfo.lowStock}}
					</view>
				</view>

				<view class="goods-choice">
					<view class="title">
						商品选择：
					</view>

					<view class="list" @touchmove.stop="moveHandle">
						<view class="item" :class="{active:goodsChoicedId===item.id}" v-for="item in goodsInfo.skuList" :key="item.id"
						 @tap="choiceGoodsType(item.id)">
							<text v-for="(item1,index) in JSON.parse(item.spData)" :key="index">
								{{item1.key}}:{{item1.value}}
							</text>
						</view>
					</view>
				</view>

				<view class="goods-choice">
					<view class="title">
						特色服务：
					</view>

					<view class="list">
						<view class="item">
							送货上门
						</view>
						<view class="item">
							上门安装
						</view>
					</view>
				</view>

				<view class="line">
				</view>

				<view class="buy-count">
					<view class="title">
						购买数量
					</view>

					<view class="count-holder">
						<view class="btn" @tap="reduceCount">
							-
						</view>
						<view class="count">
							{{count}}
						</view>
						<view class="btn" @tap="addCount">
							+
						</view>
					</view>
				</view>

				<view class="buy-btn" @tap="submit">
					确定
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		requestGoodsInfo,
		requestAddCart
	} from 'api/goodsInfo/goodsInfo.js';
	import {
		requestPreOrder
	} from 'api/user/order.js';
	import mpHtml from '@/components/mp-html/mp-html'
	export default {
		data() {
			return {
				// 弹窗显示
				popShow: false,
				goodsId: '',
				goodsInfo: {},
				count: 1,
				// 被选择的商品种类
				goodsChoicedId: '',
				addCartOrBuy: '',
				isCollect: false,
			};
		},
		methods: {
			// 删除本地收藏数据
			deleteSessionCollect() {
				let arr = uni.getStorageSync('goodsCollect');
				let index = arr.findIndex(val => {
					return val.id === this.goodsId;
				})
				arr.splice(index, 1);
				uni.showToast({
					title: '取消收藏',
					icon: 'none',
					duration: 2000,
				})
				uni.setStorageSync('goodsCollect', arr);
			},
			// 储存本地收藏数据
			setSessionCollect() {
				if (!uni.getStorageSync('goodsCollect')) {
					uni.setStorageSync('goodsCollect', [this.goodsInfo])
				} else {
					let arr = uni.getStorageSync('goodsCollect');
					arr.push(this.goodsInfo);
					uni.setStorageSync('goodsCollect', arr);
				}

				uni.showToast({
					title: '收藏成功',
					icon: 'success',
					duration: 2000,
				})
			},
			// 判断是否是收藏
			isSessionCollect() {
				if (!uni.getStorageSync('goodsCollect')) {
					return false;
				}
				let arr = uni.getStorageSync('goodsCollect');
				for (let i = 0; i < arr.length; i++) {
					if (arr[i].id === this.goodsId) {
						return true;
					}
				}

				return false;
			},

			// 添加到收藏(本地)
			changeCollect() {
				if (this.isCollect) {
					this.deleteSessionCollect();
				} else {
					this.setSessionCollect();
				}
				this.isCollect = !this.isCollect;
			},
			// 获取商品信息
			getGoodsInfo() {
				requestGoodsInfo(this.goodsId).then(res => {
					console.log(res);
					this.goodsInfo = res.data.product;
					this.goodsChoicedId = this.goodsInfo.skuList[0].id;
					uni.setNavigationBarTitle({
						title:res.data.product.name
					})
				})
			},
			// 选择商品种类
			choiceGoodsType(id) {
				this.goodsChoicedId = id;
			},
			reduceCount() {
				this.count > 1 && this.count--;
			},
			addCount() {
				this.count++;
			},
			closePop() {
				this.popShow = false;
			},
			// 添加到购物车
			addToCart() {
				this.popShow = true;
				this.addCartOrBuy = 'add';
			},
			// 立即购买
			buyNow() {
				this.popShow = true;
				this.addCartOrBuy = 'buy';
			},
			// 点击购买的结算界面
			submitToOrder() {
				if (this.count <= 0) {
					uni.showToast({
						title: '数量不能为0',
						icon: 'none',
						duration: 2000,
					})
					return false
				} else {
					requestPreOrder({
						orderItemList: [{
							productId: this.goodsId,
							productSkuId: this.goodsChoicedId,
							productQuantity: this.count
						}],
					}).then(res => {
						uni.showToast({
							title: '提交订单成功，1s后跳转',
							icon: 'none',
							duration: 1000,
						})

						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/user/cart/pre-order/pre-order?orderId=${res.data.orderId}`
							})
						}, 1000)
					})
				}

			},
			// 添加到购物车
			submitToCart() {
				requestAddCart({
					productId: this.goodsId,
					productSkuId: this.goodsChoicedId,
					quantity: this.count
				}).then(res => {
					if (res.code === 20000) {
						uni.showToast({
							title: '添加购物车成功',
							icon: 'success',
							duration: 20000
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 20000
						})
					}
				})
			},
			// 点击确认
			submit() {
				if (this.addCartOrBuy === 'add') {
					this.submitToCart();
				} else if (this.addCartOrBuy === 'buy') {
					this.submitToOrder();
				}
			},
			// 空函数
			moveHandle(e) {
				e.stopPropagation();
			},
		},
		onLoad(e) {
			this.goodsId = e.goodsId;
			this.getGoodsInfo();
			this.isCollect = this.isSessionCollect();
			
		},
		components: {
			mpHtml
		},
	}
</script>

<style lang="scss" scoped>
	.goodsinfo {
		padding-bottom: 200rpx;

		.swiper {
			width: 100%;
			height: 634rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.info-holder {
			position: relative;
			padding: 70rpx;

			.collect-btn {
				position: absolute;
				top: -50rpx;
				right: 100rpx;
				display: flex;
				background: #354e44;
				box-shadow: 0 16px 24px 0 rgba(70, 70, 70, .2);
				border-radius: 50%;
				width: 100rpx;
				height: 100rpx;

				image {
					margin: auto;
					width: 60rpx;
					height: 60rpx;
				}
			}

			.price-holder {
				display: flex;
				justify-content: space-between;

				.price-box {
					.pre.price {
						font-size: 32rpx;
					}

					.dis.price {
						margin: 0 0 0 4rpx;
						text-decoration: line-through;
						color: #b5b5b5;
						font-size: 24rpx;
						font-weight: 400;
					}

					.price {
						font-family: fantasy;
						font-weight: 600;
						font-size: 60rpx;
						line-height: 60rpx;
						color: #f2270c;
					}
				}

				.card {
					position: relative;
					padding: 0 8rpx;
					height: fit-content;
					display: inline-flex;
					margin-left: 10rpx;
					margin-top: 20rpx;
					font-size: 20rpx;
					color: #fff;
					background-color: #f2270c;
					border-color: #f2270c;
					border-radius: 10rpx;
				}

				.card::before {
					box-sizing: border-box;
					content: "";
					position: absolute;
					left: -2rpx;
					top: 50%;
					transform: translateY(-50%);
					width: 6rpx;
					height: 12rpx;
					background-color: #fff;
					border: 1px solid #ff8373;
					border-radius: 0 6rpx 6rpx 0;
					border-left: none;
				}

				.card::after {
					box-sizing: border-box;
					content: "";
					position: absolute;
					right: 0rpx;
					top: 50%;
					transform: translateY(-50%);
					width: 6rpx;
					height: 12rpx;
					background-color: #fff;
					border: 1px solid #ff8373;
					border-radius: 0 6rpx 6rpx 0;
					border-left: none;
				}
			}

			.name {
				color: #262626;
				overflow: hidden;
				font-size: 32rpx;
				font-weight: 700;
			}

			.desc {
				font-size: 24rpx;
				padding: 36rpx 0 0;
				line-height: 1.3;
				color: #666;
			}
		}


		.rich-text {
			margin: 0 auto;
			width: 614rpx;
		}

		.bottom {
			box-sizing: border-box;
			position: fixed;
			bottom: 0rpx;
			padding: 0 60rpx;
			display: flex;
			height: 120rpx;
			width: 100%;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;

			.left {
				image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.btn-holder {
				display: flex;
				align-items: center;

				.btn {
					width: 238rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					font-size: 15px;
				}

				.add-cart {
					background-color: #e2e2e2;
					color: #8f8f94;
				}

				.buy-now {
					color: #fff;
					background-color: #354e44;
				}
			}
		}

		.pop-box {
			position: absolute;
			top: 0;
			left: 0;
			display: flex;
			width: 100%;
			min-height: 100vh;
			background-color: rgba(0, 0, 0, .5);
			overflow: hidden;

			.content {
				box-sizing: border-box;
				padding: 56rpx;
				position: relative;
				margin: auto;
				margin-top: 120rpx;
				width: 628rpx;
				background-color: #fff;
				border-radius: 40rpx;

				.img-holder {
					position: absolute;
					width: 192rpx;
					height: 192rpx;
					left: 31px;
					top: -28px;
					background-color: #fff;
					border-radius: 10rpx;
					overflow: hidden;

					image {
						display: block;
						width: 100%;
						height: 100%;
					}
				}

				.close-btn {
					position: absolute;
					right: 62rpx;
					top: 62rpx;
					width: 42rpx;
					height: 42rpx;

					image {
						display: block;
						width: 100%;
						height: 100%;
					}
				}

				.name-holder {
					font-size: 28rpx;
					color: #3e3e3e;
					margin-left: 230rpx;
					margin-top: 30rpx;
					font-weight: 600;

					.stock-count {
						font-size: 24rpx;
						color: #b0b0b0;
						margin-top: 6rpx;
						line-height: 32rpx;
					}
				}

				.goods-choice {
					.title {
						margin-top: 32rpx;
						color: #3e3e3e;
						line-height: 40rpx;
						letter-spacing: 4rpx;
						font-weight: 600;
					}

					.list {
						flex-direction: column;
						height: 170rpx;
						width: 100%;
						overflow-y: auto;

						.item {
							float: left;
							margin: 5px;
							box-sizing: border-box;
							border: 2px solid transparent;
							padding-left: 5px;
							height: 30px;
							width: 500rpx;
							font-size: 13px;
							color: #3e3e3e;
							line-height: 30px;
							background: #f2f4f3;
							border-radius: 10px;

							text {
								margin-left: 10rpx;
							}
						}

						.item.active {
							border: 4rpx #354e44 solid;
						}
					}
				}

				.line {
					width: 100%;
					height: 1px;
					margin: 18rpx 0;
					background-color: #f2f4f3;
				}

				.buy-count {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.title {
						font-size: 28rpx;
						color: #3e3e3e;
						line-height: 40rpx;
						letter-spacing: 4rpx;
						font-weight: 600;
					}

					.count-holder {
						display: flex;
						justify-content: space-between;
						width: 246rpx;
						height: 60rpx;
						text-align: center;
						line-height: 60rpx;
						font-size: 30rpx;

						.btn {
							margin: 0 6rpx;
							width: 60rpx;
							height: 60rpx;
							background: #f1ece7;
							border-radius: 50%;
						}

						.count {
							margin: 0 3px;
							height: 30px;
							width: 90rpx;
							background: #f1ece7;
							border-radius: 30rpx;
						}
					}
				}

				.buy-btn {
					margin: 0 auto;
					margin-top: 32rpx;
					width: 208rpx;
					height: 80rpx;
					font-size: 32rpx;
					color: #fff;
					text-align: center;
					line-height: 80rpx;
					line-height: 80rpx;
					background-color: #354e44;
					border-radius: 28rpx;
				}
			}
		}
	}
</style>
